<template>
  <div>
    <el-tabs v-model="activeName" :stretch="true" type="card">
      <el-tab-pane label="基本信息" name="first">
        <el-table v-loading="loading" :data="productList">
          <el-table-column label="产品类型" align="center" prop="type"/>
          <el-table-column label="产品名称" align="center" prop="name"/>
          <el-table-column label="产品介绍" align="center" prop="introduce" :show-overflow-tooltip="true"/>
          <el-table-column label="产品场景" align="center" prop="scene" :show-overflow-tooltip="true"/>
          <el-table-column label="是否首页产品" align="center" prop="homePage"/>
          <el-table-column label="默认区域队长系统底价" align="center" prop="regionPricing"/>
          <el-table-column label="默认区域队长下级" align="center" prop="regionalTeamPricing"/>
          <el-table-column label="默认普通账户底价" align="center" prop="generalAccountPricing"/>
          <el-table-column label="默认推广收益" align="center" prop="promotionAdd"/>
          <el-table-column label="默认底价增加" align="center" prop="basePriceAdd"/>
          <el-table-column label="状态" align="center" key="status">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.status"
                active-value="0"
                inactive-value="1"
                @change="handleStatusChange(scope.row)">
              </el-switch>
            </template>
          </el-table-column>
          <el-table-column label="更新时间" align="center" prop="updateTime" width="100">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.updateTime, '{m}-{d} {h}:{m}') }}</span>
            </template>
          </el-table-column>
          <el-table-column label="备注" align="center" prop="remark"/>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
                v-hasPermi="['system:product:edit']">修改
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 添加或修改产品对话框 -->
        <el-dialog title="修改产品" :visible.sync="open" width="500px" append-to-body>
          <el-form ref="form" :model="form" :rules="rules" label-width="160px">
            <el-form-item label="产品名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入产品名称"/>
            </el-form-item>
            <el-form-item label="产品介绍" prop="introduce">
              <el-input v-model="form.introduce" placeholder="请输入产品介绍"/>
            </el-form-item>
            <el-form-item label="产品场景" prop="scene">
              <el-input v-model="form.scene" placeholder="请输入产品场景"/>
            </el-form-item>
            <el-form-item label="是否首页产品" prop="homePage">
              <el-input v-model="form.homePage" placeholder="请输入是否首页产品"/>
            </el-form-item>
            <el-form-item label="默认区域队长系统底价" prop="regionPricing">
              <el-input v-model="form.regionPricing" placeholder="请输入默认区域队长系统底价"/>
            </el-form-item>
            <el-form-item label="默认区域队长下级" prop="regionalTeamPricing">
              <el-input v-model="form.regionalTeamPricing" placeholder="请输入默认区域队长下级"/>
            </el-form-item>
            <el-form-item label="默认普通账户底价" prop="generalAccountPricing">
              <el-input v-model="form.generalAccountPricing" placeholder="请输入默认普通账户底价"/>
            </el-form-item>
            <el-form-item label="默认推广收益" prop="promotionAdd">
              <el-input v-model="form.promotionAdd" placeholder="请输入默认推广收益"/>
            </el-form-item>
            <el-form-item label="默认底价增加" prop="basePriceAdd">
              <el-input v-model="form.basePriceAdd" placeholder="请输入默认底价增加"/>
            </el-form-item>
            <el-form-item label="删除标志" prop="delFlag">
              <el-input v-model="form.delFlag" placeholder="请输入删除标志"/>
            </el-form-item>
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容"/>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </el-dialog>
      </el-tab-pane>
      <el-tab-pane label="定制价格" name="second">
        <customizedPrice/>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
  import {listProduct, getProduct, updateProduct} from "@/api/system/product";
  import customizedPrice from "@/views/system/product/customizedPrice.vue";
  export default {
    name: "Product",
    components: {customizedPrice},

    data() {
      return {
        activeName: 'first',
        // 遮罩层
        loading: true,
        // 产品表格数据
        productList: [],
        // 是否显示弹出层
        open: false,
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          name: [
            {required: true, message: "产品名称不能为空", trigger: "blur"}
          ],
          introduce: [
            {required: true, message: "产品介绍不能为空", trigger: "blur"}
          ],
          scene: [
            {required: true, message: "产品场景不能为空", trigger: "blur"}
          ],
        }
      };
    },
    created() {
      this.getList();
    },
    methods: {
      /** 查询产品列表 */
      getList() {
        this.loading = true;
        listProduct().then(response => {
          this.productList = response.data;
          this.loading = false;
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          type: null,
          name: null,
          introduce: null,
          scene: null,
          homePage: null,
          regionPricing: null,
          regionalTeamPricing: null,
          generalAccountPricing: null,
          promotionAdd: null,
          basePriceAdd: null,
          status: null,
          remark: null
        };
        this.resetForm("form");
      },

      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        getProduct(row.id).then(response => {
          this.form = response.data;
          this.open = true;
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            updateProduct(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        });
      },
      handleStatusChange(row) {
        let text = row.status === "0" ? "启用" : "停用";
        this.$modal.confirm('确认要"' + text + '""' + row.name + '"产品吗？').then(function () {
          return updateProduct({
            id: row.id,
            status: row.status
          });
        }).then(() => {
          this.$modal.msgSuccess(text + "成功");
        }).catch(function () {
          row.status = row.status === "0" ? "1" : "0";
        });
      }
    }
  };
</script>
